import { VTActor, VTActorOptions } from "cax-base";
import "@kitware/vtk.js/Rendering/Profiles/Geometry";
import vtk from "@kitware/vtk.js/vtk";
import vtkActor from "@kitware/vtk.js/Rendering/Core/Actor";
import vtkMapper from "@kitware/vtk.js/Rendering/Core/Mapper";
import vtkPolyData from "@kitware/vtk.js/Common/DataModel/PolyData";
import vtkAppendPolyData from "@kitware/vtk.js/Filters/General/AppendPolyData";
import vtkTriangleFilter from "@kitware/vtk.js/Filters/General/TriangleFilter";
import vtkDataArray from "@kitware/vtk.js/Common/Core/DataArray";
import vtkPoints from "@kitware/vtk.js/Common/Core/Points";
import vtkCellArray from "@kitware/vtk.js/Common/Core/CellArray";
import vtkRenderWindow from "@kitware/vtk.js/Rendering/Core/RenderWindow";
import vtkRenderer from "@kitware/vtk.js/Rendering/Core/Renderer";
import vtkRenderWindowInteractor from "@kitware/vtk.js/Rendering/Core/RenderWindowInteractor";
import vtkOpenGLRenderWindow from "@kitware/vtk.js/Rendering/OpenGL/RenderWindow";
import vtkInteractorStyleTrackballCamera from "@kitware/vtk.js/Interaction/Style/InteractorStyleTrackballCamera";
export interface GyActorOptions extends VTActorOptions {
  // * 该部位PartId
  partId: number;
  partEntityId: number;
}

export class GyActor extends VTActor {
  faceTrianglesMap: Map<any, any> = new Map<any, any>();
  partEntityId = 1;
  partId = 1; // 这个属性是GyActor独有的，MeshActor没有。
  readonly isGyActor = true;
  constructor(
    polyData: any,
    options: GyActorOptions = { partId: 1, partEntityId: 1 }
  ) {
    super(polyData, options);
    this.partId = options.partId;
    this.partEntityId = options.partEntityId;
  }
  // ? -------------- 华丽的 分割线 -------------- ?

  findFaceByTriangleIndex(index: number) {
    for (let i = 0; i < this.faceTrianglesMap.size; i++) {
      if (this.faceTrianglesMap.get(i)?.includes(index)) {
        // console.log("contains cell id.");
        return {
          faceIndex: i,
          triangleIds: this.faceTrianglesMap.get(i),
          faceId: this.faceIds[i],
        };
      }
    }
    // console.log("does not contain cell id.");
  }

  // 通过面id查询该面的三角形id
  findTriangleByFaceId(faceId: number) {
    const index = this.faceIds.indexOf(faceId);
    if (index === -1) return { faceIndex: index };
    return {
      faceIndex: index,
      triangleIds: this.faceTrianglesMap.get(index),
      faceId: this.faceIds[index],
    };
    // console.log("does not contain cell id.");
  }

  findPartByTriangleIndex(gyActors: GyActor[]) {
    for (let i = 0; i < this.faceTrianglesMap.size; i++) {
      // console.log("contains cell id.");
      return {
        partId: i,
        triangleIds: this.faceTrianglesMap.get(i),
        faceId: this.faceIds[i],
      };
    }
    // console.log("does not contain cell id.");
  }
  findGeomByTriangleIds(index: number[]) {
    for (let i = 0; i < this.faceTrianglesMap.size; i++) {
      if (this.faceTrianglesMap.get(i).includes(index)) {
        // console.log("contains cell id.");
        return {
          faceIndex: i,
          triangleIds: this.faceTrianglesMap.get(i),
          faceId: this.faceIds[i],
        };
      }
    }
    // console.log("does not contain cell id.");
  }

  hindFaces(faceIds_ins: Set<number>) {
    if (faceIds_ins.size === 0) return;
    for (let i = 0; i < this.faceIds.length; i++) {
      if (faceIds_ins.has(this.faceIds[i])) {
        const triangles = this.faceTrianglesMap.get(i);
        this.setCellsColor(triangles, [0, 0, 0, 0]);
      }
    }
  }

  unmaskFaces(faceIds_ins: Set<number>) {
    if (faceIds_ins.size === 0) return;
    for (let i = 0; i < this.faceIds.length; i++) {
      if (faceIds_ins.has(this.faceIds[i])) {
        const triangles = this.faceTrianglesMap.get(i);
        this.setCellsColor(triangles, this.defaultColor);
      }
    }
  }

  hindFaceById(faceId: number) {
    for (let i = 0; i < this.faceIds.length; i++) {
      if (this.faceIds[i] === faceId) {
        const triangles = this.faceTrianglesMap.get(i);
        this.setCellsColor(triangles, [0, 0, 0, 0]);
        return;
      }
    }
  }

  // 一个控制颜色的函数
  setDefaultFaceColor(rgb: number[]) {
    // 这里要设置一下entity默认的颜色。
    this.setDefaultColor([...rgb, 255]);
    // rgb是0-255的数字
    for (let i = 0; i < this.faceIds.length; i++) {
      const triangles = this.faceTrianglesMap.get(i);
      this.setCellsColor(triangles, [...rgb, 255]);
    }
  }
}
